package com.lezhi.socialgram.util;

import com.lezhi.socialgram.model.friend.Friend;
import com.lezhi.socialgram.model.friend.LikeMe;
import com.lezhi.socialgram.model.friend.MyLike;
import com.lezhi.socialgram.model.moments.Moment;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by daizhen on 2017/3/29.
 */
public class TableUtils {

    public interface WeekOption {
        Integer SUBTRACT = -1;
        Integer PLUS = 1;
    }

    public static class Table {
        private String tableName;

        private Date time;

        public Table(String tableName, Date time) {
            this.tableName = tableName;
            this.time = time;
        }

        public String getTableName() {
            return tableName;
        }

        public Date getTime() {
            return time;
        }
    }

    public static Table getTableNameWithDate(Class alass, Integer weekOperation) {
        if (alass == null)
            return null;
        //按星期日为间隔七天一张表
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        if (weekOperation != null) {
            instance.add(Calendar.WEEK_OF_YEAR, weekOperation);
        }
        Date date = instance.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
        //计算出后缀
        String suffix = format.format(date);
        //取出注解
        com.lezhi.socialgram.annotation.Table alassAnnotation = (com.lezhi.socialgram.annotation.Table) alass.getAnnotation(com.lezhi.socialgram.annotation.Table.class);
        //取出前缀
        String prefix = alassAnnotation.prefix();
        //拼接表名
        return new Table(prefix + suffix, date);
    }

    public static String getTableNameWithId(Class aClass, Long id, Integer size) {
        if (aClass == null)
            return null;
        String table = null;
        com.lezhi.socialgram.annotation.Table alassAnnotation = (com.lezhi.socialgram.annotation.Table)
                aClass.getAnnotation(com.lezhi.socialgram.annotation.Table.class);
        //取出前缀
        String prefix = alassAnnotation.prefix();
        if (size == 100) {
            String rs = "";
            String key = Long.toString(id);
            if (key.length() >= 2) {
                rs = key.substring(key.length() - 2);
            } else {
                key += "00";
                rs = key.substring(key.length() - 2);
            }
            table = prefix + rs;

        } else if (size == 10) {
            String key = Long.toString(id);
            key = key.substring(key.length() - 1);
            table = prefix + key;
        }
        return table;
    }

    public static void main(String[] args) {
    }

}
